const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    list: [],
    page: 1,
    loadMore: true,
    showtools: false,
    abnorData: {
      imgSrc: '/common/static/images/abnor-02.png',
      text: '你还没有任何浏览历史',
    },
    is_show_line: false,
    imgUrl: app.imgUrl,
  },

  onLoad(options) {
    let that = this;
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit
      });
    });
    that.loadDataList();
    that.today();
  },
  today() {
    var data = new Date();
    var year = data.getFullYear();
    var month = data.getMonth() + 1;
    var day = data.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (day >= 0 && day <= 9) {
      day = "0" + day;
    }
    var time = year + '-' + month + '-' + day;
    this.setData({
      today:time
    })
  },

  onReachBottom() {
    if (this.data.loadMore) {
      this.loadDataList();
    }
  },

  loadDataList() {
    let that = this;
    let { list, page } = that.data;
    app.ws_micro_shoprequest({
      url: api.user.footpoint,
      data: {
        page,
      },
      success: function(res) {
        if (res.code === 0) {
          let newlist = res.data.goods_list;
          page += 1;
          that.setData({
            list: Number(page) === 2 ? newlist : list.concat(newlist),
            page,
            loadMore: true,
            is_show_line:false
          });
        } else {
          that.setData({
            is_show_line: true
          });
        }
      },
    });
  },

  toGoodsDetail(e) {
    let { gtype, gid } = e.currentTarget.dataset;
    Number(gtype) === 1 &&
      app.util.navigateTo('/ws_micro_shop/pages/goods/goods', { id: gid });
    Number(gtype) === 2 &&
      app.util.navigateTo('/ws_micro_shop/pages/pt/details/details', { gid });
    Number(gtype) === 3 &&
      app.util.navigateTo('/ws_micro_shop/pages/miaosha/details/details', {
        id: gid,
      });
    Number(gtype) === 4 &&
      app.util.navigateTo('/ws_micro_shop/pages/book/details/details', {
        id: gid,
      });
  },

  favoriteAdd(e) {
    let { goods_id, idx } = e.currentTarget.dataset;
    let { list } = this.data.list[idx];
    let goods = 'list[' + idx + '].list';
    app.ws_micro_shoprequest({
      url: api.user.favorite_add,
      method: 'post',
      data: {
        goods_id,
      },
      success: res => {
        if (res && Number(res.code) === 0) {
          wx.showToast({
            title: '收藏成功',
            icon:'success',
            duration:1500
          });
          list.forEach(item => {
            if (Number(item.goods_id) === Number(goods_id)) {
              item.goods_info.is_favorite = 1;
            }
          });
          this.setData({
            [goods]: list,
          });
        }
      },
    });
  },

  favoriteRemove(e) {
    let { goods_id, idx } = e.currentTarget.dataset;
    let { list } = this.data.list[idx];
    let goods = 'list[' + idx + '].list';
    app.ws_micro_shoprequest({
      url: api.user.favorite_remove,
      method: 'POST',
      data: {
        goods_id,
      },
      success: res => {
        if (res && Number(res.code) === 0) {
          wx.showToast({
            title: '取消收藏成功',
            icon:'success',
            duration:1500
          });
          list.forEach(item => {
            if (Number(item.goods_id) === Number(goods_id)) {
              item.goods_info.is_favorite = 0;
            }
          });
          this.setData({
            [goods]: list,
          });
        }
      },
    });
  },

  removeHistory(e) {
    let { gid } = e.currentTarget.dataset;
    wx.showModal({
      content: '是否确认删除',
      confirmText: '确认',
      cancelText: '取消',
      success: res => {
        if (res.confirm) {
          app.ws_micro_shoprequest({
            url: api.user.deltrack,
            data: {
              ids: gid.toString(),
            },
            success: res => {
              if (res.code === 0) {
                wx.showToast({
                  title: '删除成功',
                });
                setTimeout(() => {
                  this.setData({
                    page: 1,
                    loadMore: true,
                  });
                  this.loadDataList();
                }, 1000);
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: 'none',
                });
              }
            },
          });
        }
      },
    });
  },

  favorite_ms_add(e) {
    let { goods_id, idx } = e.currentTarget.dataset;
    let { list } = this.data.list[idx];
    let goods = 'list[' + idx + '].list';
    app.ws_micro_shoprequest({
      url: api.miaosha.favorite_add,
      data: {
        id: goods_id,
      },
      success: res => {
        if (res && Number(res.code) === 0) {
          wx.showToast({
            title: '收藏成功',
          });
          list.forEach(item => {
            if (Number(item.goods_id) === Number(goods_id)) {
              item.goods_info.is_favorite = 1;
            }
          });
          this.setData({
            [goods]: list,
          });
        }
      },
    });
  },

  favorite_ms_remove(e) {
    let { goods_id, idx } = e.currentTarget.dataset;
    let { list } = this.data.list[idx];
    let goods = 'list[' + idx + '].list';
    app.ws_micro_shoprequest({
      url: api.miaosha.favorite_remove,
      data: {
        id: goods_id,
      },
      success: res => {
        if (Number(res.code) === 0) {
          wx.showToast({
            title: '取消收藏成功',
          });
          list.forEach(item => {
            if (Number(item.goods_id) === Number(goods_id)) {
              item.goods_info.is_favorite = 0;
            }
          });
          this.setData({
            [goods]: list,
          });
        }
      },
    });
  },
});